library SelectDroneCommand


	function HellfireUpgrade takes nothing returns nothing
		local unit u = GetTriggerUnit()
		local integer i = 1
		if GetItemTypeId(GetManipulatedItem())=='I013' then
			call ShiftMoveSpeed(u,-10.0)
			call IncUnitAbilityLevel(u,'A017')
			call RemoveItem(GetManipulatedItem())
			set udg_myeffect = AddSpecialEffectTarget( "Abilities\\Spells\\NightElf\\Blink\\BlinkCaster.mdl",u,"chest" )
			call PolledWait(2.5)
			call DestroyEffect(udg_myeffect)
			if udg_Mode==1 then
				call ALICEText(10.0, "Hellfire Module Installed. |cff1e90ff20 XP|r awarded to all Marines." )
				loop
					exitwhen i>udg_InitialPlayers
					set udg_Experience[udg_Sorted[i]] = udg_Experience[udg_Sorted[i]] + 20
					set i=i+1
				endloop
			endif
			if GetUnitAbilityLevel(u,'A017')==4 then
				call DestroyTrigger(GetTriggeringTrigger())
			endif
		endif
		set u = null
	endfunction

	function ClusterRocketsConditions takes nothing returns boolean
		return GetSpellAbilityId()=='A017'
	endfunction

	function ClusterRockets takes nothing returns nothing
		local location l = GetSpellTargetLoc()
		local real x = GetLocationX(l)
		local real y = GetLocationY(l)
		local rect r = Rect(x-200,y-200,x+200,y+200)
		local real dx = GetUnitX(GetTriggerUnit()) - x
		local real dy = GetUnitY(GetTriggerUnit()) - y
		local real missiletime = SquareRoot(dx*dx + dy*dy)/1000 //1000 Missile speed
		local integer i = GetConvertedPlayerId(GetTriggerPlayer())
		call PolledWait(missiletime)
		call ArtHandler(udg_Hero[i],l,1,1.0,100.0,0.2,2)
		call DestroyEffect(AddSpecialEffect("Objects\\Spawnmodels\\Other\\NeutralBuildingExplosion\\NeutralBuildingExplosion.mdl",x,y))
		call EarShockArea(x,y,450)
		call EnumItemsInRect(r,null,function killitem)
		call RemoveRect(r)
		set r = null
		call PolledWait(5.)
		call RemoveLocation(l)
		set l = null
	endfunction

	function OperatorCC takes nothing returns boolean
		return GetSpellAbilityId()=='A03E'
	endfunction

	function OperatorCCActions takes nothing returns nothing
		local location l
		local integer i = 1
		local unit u
		loop
			exitwhen i > udg_Players
			set u = udg_Hero[udg_Sorted[i]]
			set l = GetUnitLoc(u)
			if GetUnitTypeId(u)=='H000' then
				set udg_mystring = I2S(GetPlayerState(ConvertedPlayer(udg_Sorted[i]),PLAYER_STATE_RESOURCE_GOLD))
			else
				set udg_mystring = I2S(GetPlayerState(ConvertedPlayer(udg_Sorted[i]),PLAYER_STATE_RESOURCE_LUMBER))
			endif
			if IsUnitType(u,UNIT_TYPE_HERO)==true then
				call FieldChat(u,8.0, GetPlayerName(GetOwningPlayer(u)) + " : " + udg_mystring )
			endif
			call RemoveLocation(l)
			set i = i + 1
		endloop
		set l = null
		set u = null
	endfunction

	function UAVSurveillanceConditions takes nothing returns boolean
		return GetSpellAbilityId()=='A035'
	endfunction

	function SurveillanceScan takes nothing returns boolean
		return GetUnitPointValue(GetFilterUnit())==50 and GetWidgetLife(GetFilterUnit())>0.405 and GetUnitTypeId(GetFilterUnit())!='e00C'
	endfunction

	function UAVSurveillance takes nothing returns nothing
		local unit u
		local location l = GetUnitLoc(GetTriggerUnit())
		local group g = AMGetUnitsInRange(5000,l,Condition(function SurveillanceScan))
		if FirstOfGroup(g)!=null then
			call StartSound(gg_snd_HeartBeat)
		endif
		loop
			set u = FirstOfGroup(g)
			exitwhen u==null
			call PingMinimapEx(GetUnitX(u),GetUnitY(u),6.0,242,51,51,false)
			call GroupRemoveUnit(g,u)
		endloop
		call DestroyGroup(g)
		call RemoveLocation(l)
		set g = null
		set l = null
	endfunction

	function VitalSignsConditions takes nothing returns boolean
		return GetSpellAbilityId()=='A03G'
	endfunction

	function VitalSigns takes nothing returns nothing
		local integer i = 1
		local integer a = 0
		call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 8.0, "|cfff3ad00Vital Signs - 3rd Platoon, Echo Company|r" )
		loop
			exitwhen i>udg_Players
			if udg_Hero[udg_Sorted[i]]!=null then
				if GetUnitAbilityLevel(udg_Hero[udg_Sorted[i]],'B00A')>0 then
					set a = 1
					call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 8.0, udg_Pcolors[udg_Sorted[i]] + GetPlayerName(ConvertedPlayer(udg_Sorted[i])) + "|r has open wounds." )
				endif
				if GetUnitAbilityLevel(udg_Hero[udg_Sorted[i]],'Bslo')>0 then
					set a = 1
					call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 8.0, udg_Pcolors[udg_Sorted[i]] + GetPlayerName(ConvertedPlayer(udg_Sorted[i])) + "|r has a fracture." )
				endif
				if PoisonCheck(udg_Hero[udg_Sorted[i]]) then
					set a = 1
					call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 8.0, udg_Pcolors[udg_Sorted[i]] + GetPlayerName(ConvertedPlayer(udg_Sorted[i])) + "|r is poisoned." )
				endif
				if GetWidgetLife(udg_Hero[udg_Sorted[i]])<=175.0 then
					set a = 1
					if GetUnitTypeId(udg_Hero[udg_Sorted[i]])=='h00E' then
						call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 8.0, udg_Pcolors[udg_Sorted[i]] + GetPlayerName(ConvertedPlayer(udg_Sorted[i])) + "|r is badly damaged." )
					else
						call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 8.0, udg_Pcolors[udg_Sorted[i]] + GetPlayerName(ConvertedPlayer(udg_Sorted[i])) + "|r is badly injured." )
					endif
				endif
			endif
			set i=i+1
		endloop
		if a==0 then
			call DisplayTimedTextToPlayer( GetLocalPlayer(), 0, 0, 8.0, "All Marines are stable." )
		endif
	endfunction

	function PredatorMQ3Init takes unit u returns nothing
		local player p = GetOwningPlayer(u)
		local integer i = GetConvertedPlayerId(p)
		local integer mos = GetI("11",i)
		local trigger t
		set udg_PredatorPlayer = p
		call ALICEText(3.0, "|cff1e90ff" + GetPlayerName(p) + "|r is now operating the MQ-3 Predator." )
		call SetPlayerHandicap(Player(13),GetPlayerHandicap(Player(13))+0.05)
		call SetPlayerHandicap(Player(14),GetPlayerHandicap(Player(14))+0.05)
		set udg_Pace = udg_Pace-0.1
		set udg_CameraHeight[i] = 400
		set udg_CloudFactor = 200
		set udg_KillFactor[i] = 0.7
		call SelectUnitForPlayerSingle(u,p)
		if udg_Mode==2 then
			call UnitAddAbility(u,'A062')
		endif
		
		call RegisterUnitForAilmentChecks(u)
		call TriggerRegisterUnitEvent(gg_trg_Chemlights,u,EVENT_UNIT_SPELL_EFFECT)
		call TriggerRegisterUnitEvent(gg_trg_Marine_Casualty,u,EVENT_UNIT_DEATH)

		set t = CreateTrigger()
		call TriggerRegisterUnitEvent(t,u,EVENT_UNIT_SPELL_EFFECT)
		call TriggerAddCondition(t,Condition( function OperatorCC ))
		call TriggerAddAction(t,function OperatorCCActions)

		set t = CreateTrigger()
		call TriggerRegisterUnitEvent(t,u,EVENT_UNIT_SPELL_EFFECT)
		call TriggerAddCondition(t,Condition( function UAVSurveillanceConditions ))
		call TriggerAddAction(t,function UAVSurveillance)

		set t = CreateTrigger()
		call TriggerRegisterUnitEvent(t,u,EVENT_UNIT_SPELL_EFFECT)
		call TriggerAddCondition(t,Condition( function ClusterRocketsConditions ))
		call TriggerAddAction(t,function ClusterRockets)

		set t = CreateTrigger()
		call TriggerRegisterUnitEvent(t,u,EVENT_UNIT_SPELL_EFFECT)
		call TriggerAddCondition(t,Condition( function VitalSignsConditions ))
		call TriggerAddAction(t,function VitalSigns)

		set t = CreateTrigger()
		call TriggerRegisterUnitEvent(t,u,EVENT_UNIT_PICKUP_ITEM)
		call TriggerAddAction(t,function HellfireUpgrade)

		call SetPlayerState(p,PLAYER_STATE_RESOURCE_FOOD_CAP,0)
		call PanCameraToTimedForPlayer(p,GetUnitX(u),GetUnitY(u),0)
		set t = null
	endfunction

endlibrary